package com.example.zhengchaojun.arcgis.basicesTest;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.view.SketchCreationMode;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.mapping.view.SketchStyle;
import com.example.zhengchaojun.arcgis.R;
import com.example.zhengchaojun.arcgis.basicesTest.adapter.LeandListviewAdapter;
import com.example.zhengchaojun.arcgis.listener.MapSelectOnTouchListener;

/**
 * 类描述：要素编辑（添加）
 * 作者：郑朝军 on 2018/6/6
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2018/6/6
 * 修改备注：
 */
public class FeaturyAddDeleteActivity extends BaseMapActivity
{
    private ListView m_listView;

    private SketchEditor m_sketchEditor;// 草图编辑器
    private SketchStyle m_sketchStyle;// 草图编辑风格
    private int selectItem;
    private MapSelectOnTouchListener m_mapSelectOnTouchListener;

    public int getLayoutID()
    {
        return R.layout.activity_featury_add;
    }

    @Override
    public void initView()
    {
        super.initView();
        m_listView = findViewById(R.id.listView);
        m_mapViewDelegate.updateMagnifierWihtPanMapEnabled(true);
    }

    @Override
    public void initData()
    {
        m_mapSelectOnTouchListener = new MapSelectOnTouchListener(this, m_mapView);
        m_mapView.setOnTouchListener(m_mapSelectOnTouchListener);

        ArcGISMap arcGISMap = m_mapView.getMap();

        m_sketchEditor = new SketchEditor();
        m_sketchStyle = new SketchStyle();
        m_sketchEditor.setSketchStyle(m_sketchStyle);
        m_mapView.setSketchEditor(m_sketchEditor);

        LeandListviewAdapter adapter3 = new LeandListviewAdapter(this, arcGISMap.getOperationalLayers());
        m_listView.setAdapter(adapter3);
    }

    @Override
    public void initListener()
    {
        m_listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                showShort("开始编辑");
                FeatureLayer layer = (FeatureLayer) ((LeandListviewAdapter) parent.getAdapter()).getItem(position);
                startDraw(layer.getFeatureTable().getGeometryType());
                selectItem = position;
            }
        });
    }

    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.back:// 回退
                doClickBack();
                break;
            case R.id.btn_jx:// 继续
                doClickJX();
                break;
            case R.id.clear:// 清除
                doClickClear();
                break;
            case R.id.btn_save:// 保存
                doClickEditSave();
                break;
            case R.id.btn_delete:// 删除选中的要素
                doClickDelete();
                break;
        }
    }

    private void doClickDelete()
    {
        Feature feature = m_mapSelectOnTouchListener.getSelectFeature();
        if (feature == null)
        {
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认删除当前选中要素？");
        builder.setTitle("系统提示");
        builder.setPositiveButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("确认", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Feature feature = m_mapSelectOnTouchListener.getSelectFeature();
                FeatureTable featureTable = m_mapSelectOnTouchListener.getSelectFeatureLayer().getFeatureTable();
                if (feature != null)
                {
                    if (featureTable.canDelete(feature))
                    {
                        ListenableFuture<Void> deleteFeaturesAsync = featureTable.deleteFeatureAsync(feature);
                        deleteFeaturesAsync.addDoneListener(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                showShort("要素删除成功");
                                m_mapSelectOnTouchListener.clear();//清空当前选择
                            }
                        });
                    }
                    else
                    {
                        showShort("当前选中要素无法删除");
                    }
                }
                else
                {
                    showShort("当前选中要素为空");
                }
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    private void doClickClear()
    {
        m_sketchEditor.clearGeometry();
    }

    private void doClickBack()
    {
        if (m_sketchEditor.canUndo())
        {
            m_sketchEditor.undo();
        }
        else
        {
            showShort("当前状态已经无法回退了");
        }
    }

    private void doClickJX()
    {
        if (m_sketchEditor.canRedo())
        {
            m_sketchEditor.redo();
        }
        else
        {
            showShort("当前状态已经无法继续了");
        }
    }

    private void doClickEditSave()
    {
        FeatureLayer featureLayer = (FeatureLayer) m_listView.getAdapter().getItem(selectItem);
        FeatureTable featureTable = featureLayer.getFeatureTable();
        if (featureTable.canAdd())
        {
            Feature feature = featureTable.createFeature();
            feature.setGeometry(m_sketchEditor.getGeometry());
            ListenableFuture<Void> addFeatureFuture = featureTable.addFeatureAsync(feature);
            addFeatureFuture.addDoneListener(new Runnable()
            {
                @Override
                public void run()
                {
                    showShort("添加成功");
                    m_sketchEditor.clearGeometry();
                }
            });
        }
        else
        {
            showShort("要素添加失败，图层：" + featureLayer.getName() + "不支持编辑");
        }
    }


    private void startDraw(GeometryType geometryType)
    {
        switch (geometryType)
        {
            case POINT:
            case MULTIPOINT:
                m_sketchEditor.start(SketchCreationMode.POINT);
                break;
            case POLYLINE:
                m_sketchEditor.start(SketchCreationMode.POLYLINE);
                break;
            case POLYGON:
            case ENVELOPE:
                m_sketchEditor.start(SketchCreationMode.POLYGON);
                break;
            case UNKNOWN:
                showShort("当前图层：数据类型未知");
                break;
        }
    }
}
